#include "graphicsentity.h"

#include "helpers/glextensions.h"
#include "helpers/glbuffers.h"
#include "helpers/oglhelper.h"

#include "helpers/roundedbox.h"

namespace Opl{ namespace Gui {

class GraphicsEntityPrivate{
	public:
	GraphicsEntityPrivate():
			box(0)
	{}
		~GraphicsEntityPrivate()
		{
			delete box;
		}

		Helpers::OpenGL::GLRoundedBox *box;
};

GraphicsEntity::GraphicsEntity(QGraphicsItem* parent) :
	QGraphicsItem(parent), O_PRIVATE_CREATE(GraphicsEntity)
{
	O_D(GraphicsEntity);
	d->box = new Helpers::OpenGL::GLRoundedBox(0.55f, 1.0f, 10);
}

GraphicsEntity::~GraphicsEntity()
{
	O_PRIVATE_DELETE;
}

QRectF GraphicsEntity::boundingRect() const
{
	return QRectF(0.0f, 0.0f, 0.0f, 0.0f);
}

void GraphicsEntity::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	O_D(GraphicsEntity);
	QRectF rect = boundingRect().translated(pos());
	float width = float(painter->device()->width());
	float height = float(painter->device()->height());

//	float moveToRectMatrix[] = {
//		0.5f * (right - left), 0.0f, 0.0f, 0.0f,
//		0.0f, 0.5f * (bottom - top), 0.0f, 0.0f,
//		0.0f, 0.0f, 1.0f, 0.0f,
//		0.5f * (right + left), 0.5f * (bottom + top), 0.0f, 1.0f
//	};

	painter->beginNativePainting();

//	glMatrixMode (GL_MODELVIEW);
//	glPushMatrix ();
//	glLoadIdentity ();
//	glMatrixMode (GL_PROJECTION);
	glPushMatrix ();
//	glLoadIdentity ();
//	glBegin (GL_QUADS);

	QMatrix4x4 m;
	m.translate(2.0f, 1.0f, -1.5f);
	m.scale(0.3f, 0.6f, 0.6f);
	Helpers::OpenGL::multMatrix(m);

	float f = 0.2f;
//	glVertex3f(-f, -f, -f);
//	glVertex3f(f, -f, -f);
//	glVertex3f(f, f, -f);
//	glVertex3f(-f, f, -f);
	d->box->draw();
//	glEnd ();
//	glPopMatrix ();
//	glMatrixMode (GL_MODELVIEW);
	glPopMatrix ();

	painter->endNativePainting();
}

};};
